﻿<%@ Page Language="C#"  AutoEventWireup="true" CodeFile="ShowCart.aspx.cs" Inherits="ShowCart" Title="Untitled Page" %>

<link type="text/css" href="jquery-ui/themes/ui-lightness/ui.all.css" rel="Stylesheet" />
<script type="text/javascript" src="jquery-ui/ui/ui.core.js"></script>
<script type="text/javascript" src="jquery-ui/ui/ui.dialog.js"></script>
<script type="text/javascript">
    $(document).ready(function()
    {
        $("#divDelProduct").dialog({ autoOpen: false });
	    $("#divDelProduct").dialog(
            { 
                
            }
 
        );
		
    });
    function delProduct(index)
    {
        $("#divDelProduct").dialog('option','buttons',
            {
                "Không": function() { 
                        $("#divDelProduct").dialog("close"); 
                    },
                "Có": function() { 
                        var url="/WebSite1/Controllers/xlDelHangTrongGio.aspx?btnDel=1&index="+index;
                        jQuery.get(url, function(data)
                        {
                            if(data == "-1")
                                window.location = "Cart.aspx";
                            else
                                $("#divGioHangCondition").html(data);
                        });
                        $("#divDelProduct").dialog("close");
            } 
        });
        $("#divDelProduct").dialog('option','title','Bạn có muốn xóa sản phẩm?');
        $("#divDelProduct").dialog("open");
    }
    function changeQuantity(index)
    {
        var newQuantity = document.getElementById("txtSoLuong" + index).value;
        if(isNaN(newQuantity) == true)
        {
            alert("Bạn phải nhập số");
            return;
        }
        var url = "/WebSite1/Controllers/xlThayDoiSoLuong.aspx?changeState=1&index=" + index + "&newQuantity=" + newQuantity;
        jQuery.get(url, function(data)
        {
             $("#divGioHangCondition").html(data);
        });
    }
</script>
<%
    if (Session["GioHang"] == null)
    {  
%>
    <table width="100%" border="0" align="center" cellpadding="4" cellspacing="0">
      <tr>
        <td class="table_header">Giỏ hàng</td>
      </tr>
      <tr>
        <td align="center"><b>Hiện chưa có sản phẩm nào trong giỏ hàng.</b></td>
      </tr>
      <tr>
        <td align="center"><a href="Default.aspx" class="linkStyle">Quay về trang chủ?</a></td>
      </tr>
    </table>
<%
    }
    else
    {
%>
<div id="divGioHangCondition">
    <table width="100%" border="0" align="center" cellpadding="4" cellspacing="0">
      <tr>
        <td class="table_header">Giỏ hàng</td>
      </tr>
      <tr>
        <td>
        	<br />
        	<table width="95%" border="1" align="center" cellpadding="4" cellspacing="0" id="table_GioHang">
              <tr>
                <td class="giohang_Header">STT</td>
                <td class="giohang_Header">Tên sản phẩm</td>
                <td class="giohang_Header">Số lượng</td>
                <td class="giohang_Header">Đơn giá (ngàn)</td>
                <td class="giohang_Header">Khuyến mãi</td>
                <td class="giohang_Header">Giảm giá</td>
                <td class="giohang_Header">Thành tiền (ngàn)</td>
                <td class="giohang_Header">Xóa</td>
              </tr>
            <% DTO.GioHangDto GioHang = (DTO.GioHangDto)Session["GioHang"];
               double ThanhTien = 0;
               double TongThanhTien = 0;
               int j;
               for (int i = 0; i < GioHang.lstHang.Count; i++ )
               {
                   j = i + 1;
                   ThanhTien = GioHang.lstHang[i].SoLuong * (GioHang.lstHang[i].DonGia - GioHang.lstHang[i].DonGia * GioHang.lstHang[i].RateGiamGia / 100 - GioHang.lstHang[i].DonGia * GioHang.lstHang[i].RateKhuyenMai / 100);
                   TongThanhTien += ThanhTien;
                   Response.Write("<tr><td>" + j + "</td><td><a href='Details.aspx?itemID=" + GioHang.lstHang[i].ID + "'><img class='hangTrongGio' src='" + GioHang.lstHang[i].UrlHinh + "' alt='' border='0'/></a><div style='padding-top:20px'><a class='linkStyle' href='Details.aspx?itemID=" + GioHang.lstHang[i].ID + "'>" + GioHang.lstHang[i].TenSanPham + "</a></div></td><td><input type='text' size='2' ID='txtSoLuong" + i + "' value='" + GioHang.lstHang[i].SoLuong + "' Width='30px' onKeyUp='changeQuantity(" + i + ");'/></td><td>" + GioHang.lstHang[i].DonGia + "</td><td class='validMessage'>" + GioHang.lstHang[i].RateKhuyenMai + "%</td><td class='validMessage'>" + GioHang.lstHang[i].RateGiamGia + "%</td><td>" + ThanhTien + "</td><td><img src='images/cancel_48.png' width='20' height='20' alt='' onclick='delProduct(" + i + ");' /></td></tr>");
               }
               GioHang.ThanhTien = TongThanhTien;
               Response.Write("<tr><td colspan='4' class='totalCostText'>Tổng thành tiền:</td><td class='totalCost' colspan='3'>" + GioHang.ThanhTien + "</td><td></td></tr>");
               Session["GioHang"] = GioHang;
               %>
            </table>        
        </td>
      </tr>
    </table>
</div>
<div id='divDelProduct'></div>
<form id="frmCart" runat="server" method="post" action="">
    <table width="100%">
        <tr>
            <td>
                <hr /><br /><br />
                <div style="text-align:center">
                    <asp:Button runat="server" ID="btnSubmit" Text="Xác nhận thông tin" 
                        onclick="btnSubmit_Click" />&nbsp;
                    <asp:Button runat="server" ID="btnDelGioHang" Text="Hủy giỏ hàng" 
                        onclick="btnDelGioHang_Click" />&nbsp;
                    <asp:Button runat="server" ID="btnContDatHang" Text="Tiếp tục chọn hàng" 
                        onclick="btnContDatHang_Click" />
                </div>
                <br /><br />
            </td>
        </tr>
    </table> 
</form>
<%
    }
%>
